/*
 * Copyright 2009 IT Mill Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.mindtree;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import com.tw.trips.domain.Location;
import com.tw.trips.domain.Traveller;
import com.tw.trips.service.LocationService;
import com.tw.trips.service.TravellerService;
import com.tw.trips.service.helpers.DatabasePopulator;
import com.tw.trips.ui.MainPage;
import com.tw.trips.ui.mock.LocationMock;
import com.vaadin.Application;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.Window;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
@Configurable(preConstruction = true)
public class MyVaadinApplication extends Application {
	
	private Window mainWindow;
	
	@Autowired
	private TravellerService travellerService; 

	@Autowired
	private LocationService locationService;
	
	@Autowired
	DatabasePopulator dataHelper;
	
	@Override
	public void init() {
		setTheme("runo");
		mainWindow = new Window("MyTrips!");
		mainWindow.getContent().setSizeFull();
		
		
		//LocationMock locationMock = new LocationMock();
		//MainPage mainPage = new MainPage(locationMock.getLocation());
		
		mainWindow.addComponent(getLoginForm());
		
		setMainWindow(mainWindow);
	}
	
	public LoginForm getLoginForm() {
		LoginForm login = new LoginForm();
        login.addListener(new LoginForm.LoginListener() {
            public void onLogin(LoginEvent event) {
            	String userName = event.getLoginParameter("username");
            	String password = event.getLoginParameter("password");
            	if(userName!=null && userName.equalsIgnoreCase("load")) {
            		dataHelper.populateDatabase();
            	}
            	Traveller traveller = travellerService.login(userName, password);
            	if(traveller != null){
            		Location curLocation = locationService.getLocation("Chennai");
                	//if login successful
//                	LocationMock locationMock = new LocationMock();
            		MainPage mainPage = new MainPage(curLocation,traveller);
            		mainWindow.removeAllComponents();
            		mainWindow.addComponent(mainPage);
            	}
            	else{
            		mainWindow.showNotification("Please Enter Valid UserName/Password");    		
            	}
            	
            }
        });
        
        return login;
	}

}
